const json2xls = require('json2xls');
const path =require('path')
const fs = require('fs')
const pinyin = require('pinyin')



function JSON2Excel(dataPath) {
  const dataJSON = require(dataPath)
  dataJSON.forEach((item) => {
    const zth = pinyin(item['商户名称'].replace(/[\(\)]/g, ''), {style: pinyin.STYLE_FIRST_LETTER}).reduce((acc, cur) => acc += cur[0], '').toUpperCase()
    item['账套号'] = zth
  })
  const xls = json2xls(dataJSON);
  const parsed = path.parse(dataPath)
  fs.writeFileSync(path.join(__dirname, './xlsx/', parsed.name + '.xlsx'), xls, 'binary');
}

module.exports = JSON2Excel